#include <stdio.h>
#include <iostream>

#include "world.h"


world::world(void)
{
	self = this;
	int i,j;
	for(j = 0; j < MAP_SIZE_HEIGHT ; j ++)
	{
		map[0][j] = new note();
		map[0][j]->getSprite()->setPosition(ccp(0*40+20,j*40+20));
		this->addChild(map[0][j]->getSprite(),0);
		map[0][j]->setnote(0,j,wall);

		map[MAP_SIZE_WIDTH - 1][j] = new note();
		map[MAP_SIZE_WIDTH - 1][j]->getSprite()->setPosition(ccp((MAP_SIZE_WIDTH - 1)*40+20,j*40+20));
		this->addChild(map[MAP_SIZE_WIDTH - 1][j]->getSprite(),0);
		map[MAP_SIZE_WIDTH - 1][j]->setnote(MAP_SIZE_WIDTH - 1,j,wall);

	}
	for(i = 1; i < MAP_SIZE_WIDTH - 1 ; i ++)
	{
		map[i][0] = new note();
		map[i][0]->getSprite()->setPosition(ccp(i*40+20,0*40+20));
		this->addChild(map[i][0]->getSprite(),0);
		map[i][0]->setnote(i,0,wall);

		map[i][MAP_SIZE_HEIGHT - 1] = new note();
		map[i][MAP_SIZE_HEIGHT - 1]->getSprite()->setPosition(ccp(i*40+20,(MAP_SIZE_HEIGHT - 1)*40+20));
		this->addChild(map[i][MAP_SIZE_HEIGHT - 1]->getSprite(),0);
		map[i][MAP_SIZE_HEIGHT - 1]->setnote(i,MAP_SIZE_HEIGHT - 1,wall);

		for(j = 1; j < MAP_SIZE_HEIGHT - 1; j ++)
		{
			map[i][j] = new note();
			map[i][j]->getSprite()->setPosition(ccp(i*40+20,j*40+20));
			this->addChild(map[i][j]->getSprite(),0);
			map[i][j]->setnote(i,j,blank);
		}
	}
}

world* world::getself()
{
	return self;
}

void world::putsnake(int x,int y,world* w)
{
	sk = new snake(x,y,w);
}

void world::putsupersnake(int x,int y,world* w)
{
	supersk = new supersnake(x,y,w);
}


void world::putfood(world* w)
{
	fd = new food(w);
}

void world::puttrap(world* w)
{
	td = new trap(w);
}




bool world::run(world *w)
{	
	if(sk->move(w) == false)
	{
		return false;
	}
	return true;
}
bool world::super_run(world *w)
{
	supersk->move(w);
	return true;
}
void world::setdirection(char d)
{
	if(d == 'w' || d == 'a' || d == 's' || d == 'd')
	{
		sk->setdr(d);
	}
}

char world::getdirection()
{
	return sk->getdr();
}

note world::getsnakehead()
{
	return sk->getheadPoint();
}

note* world::getfood()
{
	return fd->getfd();
}

int world::getscore()
{
	return sk->getsc();
}

int world::getsnakelength()
{
	return sk->getlength();
}

bool world::changeSpeed()
{
	return sk->changeSpeed();
}

void world::resetchangeSpeed()
{
	sk->resetchangeSpeed();
}

bool world::changesuperSpeed()
{
	return supersk->changeSpeed();
}

void world::resetchangesuperSpeed()
{
	supersk->resetchangeSpeed();
}
int world::getsupersnakelength()
{
	return supersk->getlength();
}
void world::gameover()
{

}

world::~world(void)
{
	delete sk;
	delete supersk;
	delete fd;
	for(int j = 0; j < MAP_SIZE_HEIGHT ; j ++)
	{
		for(int i = 1; i < MAP_SIZE_WIDTH ; i ++)
		{
			delete map[i][j];
		}
	}
}
